<template>
  <div>
    <div class="size-icon--style" @click="show()">
      <el-icon style="font-size: 25px;">
        <Message/>
      </el-icon>
      <el-badge v-if="sum>0" :value="sum" style=" margin-bottom: 25px;margin-right: 5px;"></el-badge>
    </div>
    <el-dialog v-model="open" title="消息提醒" width="800">
      <div>
        <el-table v-loading="loading" :data="messageList">
<!--          <el-table-column label="消息类型"></el-table-column>-->
          <el-table-column label="流转意见" prop="circulationSuggestion"/>
          <el-table-column label="项目名称" prop="projectName"/>
          <el-table-column label="用户" prop="userName"/>
          <el-table-column label="发送时间" prop="circulationDate"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <div v-if="scope.row.status==0">
                <el-button type="primary" size="small"  style="margin: 4px;padding-left: 2px;padding-right: 2px;font-size: 15px" @click="confi(scope.row.id)"> 确认 </el-button>
                <el-button type="primary" size="small" style="margin: 4px;padding-left: 2px;padding-right: 2px;font-size: 15px" @click="toPage(scope.row.circulationType,scope.row.projectId)">去查看</el-button>
              </div>
              <span v-else>已确认</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination
            v-show="total>0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        />
      </div>


    </el-dialog>
  </div>
</template>

<script setup>
import {confirmById, list, sumMsg} from "@/api/message/message.js";
import {ElNotification} from "element-plus";
import {set} from "@vueuse/core";

const open = ref(false);
const loading = ref(false);
const messageList = ref([]);
const date = ref([]);
const total = ref(0);
const sum = ref(0);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 5,
  projectName: null,
  startDate: null,
  endDate: null
});

function reset() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 5,
    projectName: null,
    startDate: null,
    endDate: null
  }
}

function getList() {
  loading.value = true;
  list(queryParams).then(res => {
    messageList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  })
}

function confi(id) {
  confirmById(id).then(res => {
    ElNotification({
      title: '成功',
      message: '已确认消息',
      type: 'success',
    })
    getList();
  })
}


function toPage(post,pid){
  open.value=false;
  switch (post){
    case 6:
      //样品管理员
      router.push('/sample/sitem');
      break;
    case 9:
      //实验管理员
        router.push('/experiment/eexperiment');
      break;
    case 7:
      //实验分发员
      router.push('/experiment/eout');
      break;
    case 8:
      //项目负责人
      router.push('/project/handle');
      break;
    case 10:
      //财务
      router.push('/finance');
      break;
  }
}

function show() {
  open.value = true;
  getList()
}
sumMsg().then(res => {
  sum.value = res.data;
})

//监视器
const warchQueryDate = watch(date, (newVal) => {
  if (newVal == null) {
    queryParams.value.startDate = null;
    queryParams.value.endDate = null;
    return;
  }
  if (newVal.length == 2) {
    queryParams.value.startDate = newVal[0];
    queryParams.value.endDate = newVal[1];
  }
})
//定时器
setInterval(() => {
  sumMsg().then(res => {
    sum.value = res.data;
  })
}, 300000)
</script>

<style scoped lang="scss">
.size-icon--style {
  line-height: 30px;
  padding-top: 10px;
}

</style>